//
//  NewFeatureViewCell.swift
//  WeiBoSwift
//
//  Created by rong on 15/6/28.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit

class NewFeatureViewCell: UICollectionViewCell {
    
    //
    var imgIndex: Int = 0{
        
        didSet {
            
            iconView.image = UIImage(named: "new_feature_\(imgIndex + 1)")
            
        }
    }
    
    //控件
    //图片
    lazy var iconView: UIImageView = {
    
        let icon =  UIImageView()
        icon.frame = self.bounds
        
        return icon
    }()
    
    //按钮
    lazy var startBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.frame = CGRectMake(0, 0, 105, 36)
        
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        //“开始体验”按钮点击事件
        btn.addTarget(self, action: "start", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    func start()
    {
        //true ,是跳过welcome，直接显示mainviewcontroller
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotication, object: true)
    }
    
    //显示开始按钮
    func showStartBtn() {
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.userInteractionEnabled = false
        
      /*  UIView.animateWithDuration(0.8, animations: { () -> Void in
            //相当于将view的transform重置，恢复成默认状态
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startBtn.userInteractionEnabled = true
        }*/
        
        /*
        在IOS7开始，系统动画效果广泛应用Spring Animation ：
        
        [UIView animateWithDuration:4.0 // 动画时长
        delay:0.0 // 动画延迟
        usingSpringWithDamping:1.0 // 类似弹簧振动效果 0~1
        initialSpringVelocity:5.0 // 初始速度
        options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
        animations:^{
        // code...
        CGPoint point = _imageView.center;
        point.y += 150;
        [_imageView setCenter:point];
        } completion:^(BOOL finished) {
        // 动画完成后执行
        // code...
        [_imageView setAlpha:1];
        }];
        usingSpringWithDamping：它的范围为 0.0f 到 1.0f ，数值越小「弹簧」的振动效果越明显。
        
        initialSpringVelocity：初始的速度，数值越大一开始移动越快。值得注意的是，初始速度取值较高而时间较短时，也会出现反弹情况。
        */
        
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }, completion: { (_) -> Void in
                print("OK")
                self.startBtn.userInteractionEnabled = true
        })

    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(iconView)
        addSubview(startBtn)
        
        startBtn.hidden = true
        
        //设置布局
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
    }
    
    required init(coder aDecoder: NSCoder) {
//        print("NewFeatureViewCell: required init(coder aDecoder: NSCoder)")
        fatalError("init(coder:) has not been implemented")
    }
}
